﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.IO;

namespace CommunitySystem.Infrastructure.CommonMethod
{
    public class LayoutMethod
    {
        /// <summary>
        /// 每次如果有相同的tabid， 则不新增tab页，
        /// </summary>
        /// <param name="uc"></param>
        /// <param name="header"></param>
        /// <param name="tabid"></param>
        public static   void AddTabItem(UserControl uc, string header, string tabid, TabControl Control)
        {
            var item = Control.Items.Where(c => ((TabItem)c).Name.Equals(tabid)).FirstOrDefault();
            if (item != null)
            {
                Control.SelectedItem = item;
                return;
            }
            Grid g = new Grid();
            RowDefinition row = new RowDefinition();
            g.RowDefinitions.Add(row);
            ColumnDefinition c1 = new ColumnDefinition();
            ColumnDefinition c2 = new ColumnDefinition();
            ColumnDefinition c3 = new ColumnDefinition();
            c2.Width = new GridLength(12);
            c3.Width = new GridLength(14);
            g.ColumnDefinitions.Add(c1);
            g.ColumnDefinitions.Add(c2);
            g.ColumnDefinitions.Add(c3);

            TextBlock disName = new TextBlock();
            disName.Text = header;//"菜单管理";
            disName.FontSize = 13;
            disName.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            disName.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            disName.Foreground = new SolidColorBrush(Color.FromArgb(255, 073, 068, 068));
            disName.SetValue(Grid.ColumnProperty, 0);
            disName.SetValue(Grid.RowProperty, 0);

            Image img = new Image();
            img.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            img.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            var isc = new ImageSourceConverter();
            img.Source = (ImageSource)isc.ConvertFromString("../../Images/UI/blue_velvet_020.png");
            img.Width = 14;
            img.Height = 14;
            img.SetValue(Grid.ColumnProperty, 2);
            img.SetValue(Grid.RowProperty, 0);
            img.MouseLeftButtonDown += (sender, e) =>
            {
                Control.Items.Remove(Control.SelectedItem);
            };
            img.Cursor = Cursors.Hand;

            g.Children.Add(disName);
            g.Children.Add(img);

            TabItem tabItem = new TabItem() { Name = tabid };
            tabItem.Content = uc;
            tabItem.Header = g;//title;
            Control.Items.Add(tabItem);
            Control.SelectedItem = tabItem;
        }


        public static void AddTreeViewItem<T>(TreeView tv, ObservableCollection<T> items, int rootpid)
        {
            tv.Items.Clear();
            foreach (T item in items.Where(i => i.GetType().GetProperty("PID").GetValue(i, null).Equals(rootpid)))
            {
                TreeViewItem tvi = new TreeViewItem();
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Width = 150, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                if (item.GetType().GetProperty("icon").GetValue(item, null) != null && (item.GetType().GetProperty("icon").GetValue(item, null) as byte[]).Length > 0)
                {
                    Image img = new Image() { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, Stretch = Stretch.Fill, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write(item.GetType().GetProperty("icon").GetValue(item, null) as byte[], 0, (item.GetType().GetProperty("icon").GetValue(item, null) as byte[]).Length);
                    b.SetSource(mm);
                    img.Source = b;
                    using (MemoryStream stream = new MemoryStream(item.GetType().GetProperty("icon").GetValue(item, null) as byte[]))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        BitmapImage b2 = new BitmapImage();
                        b2.SetSource(stream);
                        img.Source = b2;
                    }
                    sp.Children.Add(img);
                }
                sp.Children.Add(new TextBlock() { Text = item.GetType().GetProperty("Title").GetValue(item, null).ToString(), FontSize = 12 });
                tvi.Header = sp;
                tvi.DataContext = item;
                AddChildNode(tvi, item, items);
                
                tv.Items.Add(item);
            }
        }

       private static  void AddChildNode<T>(TreeViewItem pitem,  T pmenu, ObservableCollection<T> source)
        {
            foreach (var i in source.Where(c => c.GetType().GetProperty("PID").GetValue(c, null).Equals(pmenu.GetType().GetProperty("ID").GetValue(pmenu, null))))
            {
                TreeViewItem item = new TreeViewItem();
                //item.SetBinding(TreeViewItem.HeaderProperty, new System.Windows.Data.Binding() { Path = new PropertyPath("Title") });
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Width = 150, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                if (i.GetType().GetProperty("icon").GetValue(i, null) != null && (i.GetType().GetProperty("icon").GetValue(i, null) as byte[]).Length > 0)
                {
                    Image img = new Image();
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write((i.GetType().GetProperty("icon").GetValue(i, null) as byte[]), 0, (i.GetType().GetProperty("icon").GetValue(i, null) as byte[]).Length);
                    b.SetSource(mm);
                    img.Source = b;
                    sp.Children.Add(img);
                }
                sp.Children.Add(new TextBlock() { Text = (i.GetType().GetProperty("Titile").GetValue(i, null) as byte[]).ToString(), FontSize = 12 });
                item.Header = sp;
                item.DataContext = i;
                AddChildNode(item, i, source);              
                pitem.Items.Add(item);
            }
        }
    }
}
